package com.example.fp.zlz.util;

import com.example.fp.zlz.entity.Admin;
import com.example.fp.zlz.mapper.AdminMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Component("myAuthenticationSuccessHandler")
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private AdminMapper adminMapper;
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException{
//		super.onAuthenticationSuccess(request,response,authentication);
		//这里可以根据实际情况，来确定是跳转到页面或者json格式。
		//如果是返回json格式，那么我们这么写
		System.out.println(request.getRequestedSessionId());
		Map<String,Object> map=new HashMap<>();
		map.put("code", "200");
		map.put("msg", "登录成功");
		map.put("sessionid",request.getRequestedSessionId());
		Admin admin=new Admin();
		admin.setUsername(authentication.getName());
		admin.setToken(request.getRequestedSessionId());
		admin.setOnline(true);
		adminMapper.updatetokenbyname(admin);
		map.put("userinfo",adminMapper.selectbyusername(authentication.getName()));
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().write(objectMapper.writeValueAsString(map));


		//如果是要跳转到某个页面的，比如我们的那个whoim的则
		//new DefaultRedirectStrategy().sendRedirect(request, response, "/index");
	}
}
